<?php

/**
 * aplicaciones actions.
 *
 * @package    auip
 * @subpackage aplicaciones
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class aplicacionesActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    //$this->aplicacion_becas = Doctrine_Core::getTable('AplicacionBeca')
    //  ->createQuery('a')
    //  ->execute();
    //  $this->getUser()->setAttribute('email', Mensajero::desecriptar($request->getParameter('email')));
      $this->aplicacion_becas = AplicacionBecaTable::instancia($this->getUser()->getAttribute('email'));
      
      if($request->hasParameter('id'))
      {
          $this->getUser()->setAttribute('id', $request->getParameter('id'));
          //$this->forward('aplicaciones', 'new');
          $this->redirect('aplicaciones/new');
      }
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->aplicacion_beca = Doctrine_Core::getTable('AplicacionBeca')->find(array($request->getParameter('beca_id'),
                                    $request->getParameter('usuario_id')));
    $this->forward404Unless($this->aplicacion_beca);
  }

  public function executeNew(sfWebRequest $request)
  {         
        $this->aplicacion_beca = new AplicacionBeca();        
        $this->aplicacion_beca->setBecaId($this->getUser()->getAttribute('id'));
        $this->aplicacion_beca->setUsuarioId($this->getUser()->getAttribute('email'));        
        $this->forward404Unless($this->aplicacion_beca);        
        $this->form = new AplicacionBecaForm($this->aplicacion_beca);               
        $this->getUser()->setAttribute('nom_beca', BecaTable::getBecaEncontrada($this->getUser()->getAttribute('id'))->getNombre());
        $this->getUser()->setAttribute('nom_uni', BecaTable::getBecaEncontrada($this->getUser()->getAttribute('id'))->getUniversidad()->getNombre());
        $this->getUser()->setAttribute('tipo_beca_id', BecaTable::getBecaEncontrada($this->getUser()->getAttribute('id'))->getTipoBeca()->getId());
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    
    $this->form = new AplicacionBecaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($aplicacion_beca = Doctrine_Core::getTable('AplicacionBeca')->find(array($request->getParameter('beca_id'),
              $request->getParameter('usuario_id'))), sprintf('Object aplicacion_beca does not exist (%s).', $request->getParameter('beca_id'),
              $request->getParameter('usuario_id')));
    $this->form = new AplicacionBecaForm($aplicacion_beca);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($aplicacion_beca = Doctrine_Core::getTable('AplicacionBeca')->find(array($request->getParameter('beca_id'),
              $request->getParameter('usuario_id'))), sprintf('Object aplicacion_beca does not exist (%s).', $request->getParameter('beca_id'),
              $request->getParameter('usuario_id')));
    $this->form = new AplicacionBecaForm($aplicacion_beca);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($aplicacion_beca = Doctrine_Core::getTable('AplicacionBeca')->find(array($request->getParameter('beca_id'),
              $request->getParameter('usuario_id'))), sprintf('Object aplicacion_beca does not exist (%s).', $request->getParameter('beca_id'),
              $request->getParameter('usuario_id')));
    $aplicacion_beca->delete();

    $this->redirect('aplicaciones/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));    
    if ($form->isValid())
    {
      $aplicacion_beca = $form->save();
      //$this->redirect('aplicaciones/edit?beca_id='.$aplicacion_beca->getBecaId().'&usuario_id='.$aplicacion_beca->getUsuarioId());
      $this->redirect('documentos/new');
    }
  }
}
